package com.wechat.utils;

import com.alibaba.fastjson.JSONObject;

/**
 *
 * @author cgc
 * @date 2018/4/10
 */
public class ActionResult<T> extends JSONObject {
    public ActionResult() {
        this.put((String)"success", Boolean.valueOf(true));
    }

    public ActionResult(boolean success) {
        this.put((String)"success", Boolean.valueOf(success));
    }

    public ActionResult(boolean success, String message) {
        this.setSuccess(Boolean.valueOf(success));
        this.setMessage(message);
    }

    public ActionResult setCode(Integer code) {
        return this.put((String)"code", code);
    }

    public ActionResult setSuccess(Boolean success) {
        if(success.booleanValue()) {
            this.setCode(Integer.valueOf(200));
        } else {
            this.setCode(Integer.valueOf(500));
        }

        return this.put((String)"success", success);
    }

    public ActionResult setLoginTimeout(Boolean loginTimeout) {
        if(loginTimeout.booleanValue()) {
            this.setCode(Integer.valueOf(600));
        }
        return this.put((String)"loginTimeout", loginTimeout);
    }

    public ActionResult success() {
        return this.setSuccess(Boolean.valueOf(true));
    }

    public ActionResult failure() {
        return this.setSuccess(Boolean.valueOf(false));
    }

    public ActionResult setTitle(String title) {
        return this.put((String)"title", title);
    }

    public ActionResult setUrl(String url) {
        return this.put((String)"url", url).setCode(Integer.valueOf(301));
    }

    public ActionResult setData(T data) {
        return this.put("data", data).setCode(Integer.valueOf(200));
    }

    @Override
    public ActionResult put(String key, Object value) {
        super.put(key, value);
        return this;
    }

    public boolean isSuccess() {
        return this.getBoolean("success").booleanValue();
    }

    public ActionResult setMessage(String message) {
        return this.put((String)"message", message);
    }

    public String toJson() {
        return this.toJSONString();
    }

}